package com.bbx.taxi.client.Task;

import android.app.Activity;
import android.os.AsyncTask;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.official.passanger.Official_OrgPathBuild;
import com.bbx.api.sdk.model.official.passanger.Return.Official_OrgPath;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Official_OrgPathNet;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;

/** 
 * @Path com.bbx.taxi.client.Task.MyORGPathTask.java
 * @Author 严旭文
 * @Date 2016-4-11 下午4:14:34
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 获取我的机构路径
 */
public class MyORGPathTask{
	private Activity context;
	public static MyORGPathTask instance;
	public ORGPathTask mORGPathTask;
	public interface OnOrgPathListener {
		/**
		 *获取我的机构路径
		 */
		public void onOrgPathFinish(Official_OrgPath mOfficial_OrgPath);
	}

	static OnOrgPathListener mOnOrgPathListener = null;

	public static void setOnOrgPathListener(OnOrgPathListener l) {
		mOnOrgPathListener = l;
	}
	public static MyORGPathTask getInstance(Activity context,String org_id,int direct) {
		instance = new MyORGPathTask(context,org_id,direct);
		return instance;
	}
	public MyORGPathTask(Activity context,String org_id,int direct) {
		this.context = context;
		getOrgPath(org_id,direct);
	
	}
	/**
	 * 获取用户机构路径
	 */
	public void getOrgPath(String org_id,int direct) {
		Official_OrgPathBuild mOfficial_OrgPathBuild=new Official_OrgPathBuild(context);
		mOfficial_OrgPathBuild.opuid=MyApplication.getInstance().getUid();
		mOfficial_OrgPathBuild.org_id=org_id;
		mOfficial_OrgPathBuild.direct=direct;
		ORGPathTask mORGPathTask=new ORGPathTask(context, mOfficial_OrgPathBuild);
		mORGPathTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	private class ORGPathTask extends BaseAsyncTask {

		private Official_OrgPathBuild mOfficial_OrgPathBuild;

		public ORGPathTask(Activity context, Official_OrgPathBuild mOfficial_OrgPathBuild) {
			super(context);
			this.mOfficial_OrgPathBuild = mOfficial_OrgPathBuild;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				Official_OrgPath mOfficial_OrgPath=(Official_OrgPath)object;
				if(mOfficial_OrgPath!=null){
					if (mOnOrgPathListener != null) {
						mOnOrgPathListener.onOrgPathFinish(mOfficial_OrgPath);
					}
				}else{
					ToastUtil.showToast(context, R.string.no_network);
					if (mOnOrgPathListener != null) {
						mOnOrgPathListener.onOrgPathFinish(null);
					}
				}
				
			} else {
				ToastUtil.showToast(context, R.string.no_network);
				if (mOnOrgPathListener != null) {
					mOnOrgPathListener.onOrgPathFinish(null);
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context, R.string.no_network);
			if (mOnOrgPathListener != null) {
				mOnOrgPathListener.onOrgPathFinish(null);
			}
	
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOfficial_OrgPathBuild).getJson();
			return new Official_OrgPathNet(context, json);
		}
	}

}
